package com.gxkj.drm.service;

import com.gxkj.drm.dao.PushUserDao;
import com.gxkj.drm.dao.PushUserOperateLogDao;
import com.gxkj.drm.entitys.PushUser;
import com.gxkj.drm.entitys.PushUserOperateLog;
import com.gxkj.drm.enums.RoleType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 用户服务
 * @author shaoxiangfei
 *
 */
@Service
public class UserService {

    @Resource
    private PushUserDao pushUserDao;

    @Resource
    private PushUserOperateLogDao pushUserOperateLogDao;

    /**
     * 新增用户
     * @param user
     * @return
     */
    public int addUser(PushUser user) {
        return this.pushUserDao.addUser(user);
    }

    /**
     * 获取用户名
     * @param name
     * @return
     */
    public PushUser getUserByName(String name) {
        return this.pushUserDao.getUserByName(name);
    }

    /**
     * 添加用户操作日志
     * @param pushUserOperateLog
     */
    public void addUserOperateLog(PushUserOperateLog pushUserOperateLog) {
        this.pushUserOperateLogDao.addUserOperateLog(pushUserOperateLog);
    }

    /**
     * 获取用户操作记录
     * limit 100
     * @return
     */
    public List<PushUserOperateLog> getUserOperateLogList(PushUser user) {
        RoleType role = RoleType.find(user.getLevel());
        if (role != null && role.havePermission(RoleType.ADMIN)) {
            return pushUserOperateLogDao.getAllOperateLogList();
        } else {
            String clusters = user.getClusters();
            if (StringUtils.isNotBlank(clusters)) {
                List<String> clusterList = Arrays.asList(clusters.split(","));
                return this.pushUserOperateLogDao.getUserOperateLogList(clusterList);
            }
        }
        return null;
    }

    /**
     * 获取当前权限下的全部用户信息
     * @param level 当前用户的级别
     * @return
     */
    public List<PushUser> getAllUser(int level) {
        return this.pushUserDao.getAllUser(level);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    public int updateUser(PushUser user) {
        return pushUserDao.updateUser(user);
    }


    public int deleteUserByName(String name) {
        return pushUserDao.deleteUserByName(name);
    }
}